import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const modulesFiles = require.context('./modules', true, /\.js$/)

const modulesRouter = [];
modulesFiles.keys().reduce((modules, modulePath) => {
    const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1')
    const value = modulesFiles(modulePath)
    modules[moduleName] = value.default;
    modules && modules[moduleName] && modules[moduleName].map((modulesItem) => {
        modulesRouter.push(modulesItem);
    });
    console.log(modules, "modules", modules[moduleName], modulesRouter);
    return modules
}, {});

const router = new VueRouter({
    routes: modulesRouter
})

export default router;